package com.bitdf.txing.order.config;//package com.atguigu.gulimall.order.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

@Configuration
public class OrderMyMQConfig {

    /**
     * 原本是放在 com.bitdf.txing.order.config.MyRabbitConfig中，不过会引起循环依赖， 所以就移出来了
     * 原因：myRabbitConfig ==》rabbitTemplate ==》rabbitTemplateConfigurer （它需要一个转换器，而我们配置的就是转换器，因此引起循环依赖了）
     * @return
     */
    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

//    @Bean
//    public Queue orderDelayQueue(){
////        x-dead-letter-exchange: order-event-exchange
////        x-dead-letter-routing-key: order.release.order
////        x-message-ttl: 60000
//        Map<String,Object> arguments=new HashMap<>();
//        arguments.put("x-dead-letter-exchange","order-event-exchange");
//        arguments.put("x-dead-letter-routing-key","order.release.order");
//        arguments.put("x-message-ttl",60000);
//        return new org.springframework.amqp.core.Queue("order.delay.queue",true,false, false, arguments);
//    }
//    @Bean
//    public Queue orderReleaseOrderQueue(){
//        Queue queue = new Queue("order.release.order.queue", true, false, false, null);
//        return queue;
//    }
//    @Bean
//    public Exchange orderEventExchange(){
//        return new TopicExchange("order-event-exchange", true, false);
//    }
//    @Bean
//    public Binding orderCreateOrderBinding(){
//        Binding binding = new Binding("order.delay.queue", Binding.DestinationType.QUEUE, "order-event-exchange", "order.create.order", null);
//        return binding;
//    }
//    @Bean
//    public Binding orderReleaseOrderBinding(){
//        Binding binding = new Binding("order.release.order.queue", Binding.DestinationType.QUEUE, "order-event-exchange", "order.release.order", null);
//        return binding;
//    }
//
//    @Bean
//    public Binding orderReleaseOtherBinding(){
//        Binding binding = new Binding("stock.release.stock.queue", Binding.DestinationType.QUEUE, "order-event-exchange", "order.release.other.#", null);
//        return binding;
//    }
//
//    @RabbitListener(queues = "order.release.order.queue")
//    public void listener(OrderEntity entity, Channel channel, Message message) throws IOException {
//        System.out.println("收到过期的订单信息："+entity.getOrderSn());
//        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
//    }
    /* 容器中的Queue、Exchange、Binding 会自动创建（在RabbitMQ）不存在的情况下 */

    /**
     * 死信队列
     *
     * @return
     */@Bean
    public Queue orderDelayQueue() {
        /*
            Queue(String name,  队列名字
            boolean durable,  是否持久化
            boolean exclusive,  是否排他
            boolean autoDelete, 是否自动删除
            Map<String, Object> arguments) 属性
         */
        HashMap<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", "order-event-exchange");
        arguments.put("x-dead-letter-routing-key", "order.release.order");
        arguments.put("x-message-ttl", 30*1000); // 消息过期时间 1分钟
        Queue queue = new Queue("order.delay.queue", true, false, false, arguments);

        return queue;
    }

    /**
     * 释放订单队列
     *
     * @return
     */
    @Bean
    public Queue orderReleaseQueue() {

        Queue queue = new Queue("order.release.order.queue", true, false, false);

        return queue;
    }

    /**
     * TopicExchange
     *  订单创建交换机
     * @return
     */
    @Bean
    public Exchange orderEventExchange() {
        /*
         *   String name,
         *   boolean durable,
         *   boolean autoDelete,
         *   Map<String, Object> arguments
         * */
        return new TopicExchange("order-event-exchange", true, false);

    }

    /**
     * 绑定 创建完订单 ==》 订单交换机 ==》 死信队列
     * @return
     */
    @Bean
    public Binding orderCreateBinding() {
        /*
         * String destination, 目的地（队列名或者交换机名字）
         * DestinationType destinationType, 目的地类型（Queue、Exhcange）
         * String exchange,
         * String routingKey,
         * Map<String, Object> arguments
         * */
        return new Binding("order.delay.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.create.order",
                null);
    }

    /**
     * 绑定 死信队列 ===》 下单交换机 ===》 关闭订单队列
     * @return
     */
    @Bean
    public Binding orderReleaseBinding() {

        return new Binding("order.release.order.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.order",
                null);
    }

    /**
     * 订单释放直接和库存释放进行绑定
     * @return
     */
    @Bean
    public Binding orderReleaseOtherBinding() {

        return new Binding("stock.release.stock.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.other.#",
                null);
    }

// ============================= 秒杀 ======================================
    /**
     * 商品秒杀队列
     * @return
     */
    @Bean
    public Queue orderSecKillOrrderQueue() {
        Queue queue = new Queue("order.seckill.order.queue", true, false, false);
        return queue;
    }

    /**
     * TopicExchange
     *  秒杀交换机
     * @return
     */
    @Bean
    public Exchange execSeckillExchange() {
        /*
         *   String name,
         *   boolean durable,
         *   boolean autoDelete,
         *   Map<String, Object> arguments
         * */
        return new TopicExchange("exec-seckill-exchange", true, false);
    }

    /**
     * 绑定
     * @return
     */
    @Bean
    public Binding orderSecKillOrrderQueueBinding() {
        //String destination, DestinationType destinationType, String exchange, String routingKey,
        // 			Map<String, Object> arguments
        Binding binding = new Binding(
                "order.seckill.order.queue",
                Binding.DestinationType.QUEUE,
                "exec-seckill-exchange",
                "order.seckill.order",
                null);

        return binding;
    }
}
